#include<bits/stdc++.h>
using namespace std;
const int N=1000005;

int n,k;
int arr[N];
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i]);
    }
    deque<int> num,index;
    for(int i=1;i<=n;i++){
        while(!num.empty()&&arr[i]<=num.back()){
            num.pop_back();
            index.pop_back();
        }
        num.push_back(arr[i]);
        index.push_back(i);
        if(i>=k){
            printf("%d ",num.front());
        }
        while(!index.empty()&&index.front()<=i+1-k){
            num.pop_front();
            index.pop_front();
        }
    }
    printf("\n");
    num.clear();
    index.clear();
    for(int i=1;i<=n;i++){
        while(!num.empty()&&arr[i]>=num.back()){
            num.pop_back();
            index.pop_back();
        }
        num.push_back(arr[i]);
        index.push_back(i);
        if(i>=k){
            printf("%d ",num.front());
        }
        if(index.front()<=i+1-k){
            num.pop_front();
            index.pop_front();
        }
    }
    return 0;
}
